<?php
// ensure this file is being included by a parent file
defined( '_VALID_MOS' ) or defined( '_JEXEC' ) or die( 'Restricted access' );

class niktranslate {

  var $items			= array();

	var $path_ref		= NULL;
	var $filename_ref	= NULL;
  var $header_ref		= array();
  var $items_ref		= array();

	var $path_trans		= NULL;
	var $filename_trans	= NULL;
  var $header_trans	= array();
  var $items_trans	= array();
  
  function niktranslate( )
  {
#	  $this->workPath = $workPath;
  }
  
  function _split_path ( $fullpath, & $path, & $filename)
{
	$tmp = explode('/', $fullpath);
	$filename = $tmp[count($tmp)-1];
	//echo $filename;
	$path = substr( $fullpath, 0, strlen( $fullpath ) - strlen( $filename ) );
	//echo $path;
} // function
  
  function translate ($path_ref, $path_trans)
  {

		### reading default
		$this->_getContents( $this->header_ref, $this->items_ref, $path_ref );
		### reading translation
		$this->_getContents( $this->header_trans, $this->items_trans, $path_trans );
		$cnt = 0;
		
		$this->_split_path( $path_ref, $this->path_ref, $this->filename_ref );
		$this->_split_path( $path_trans, $this->path_trans, $this->filename_trans );
		
		$this->items = $this->items_ref;
		
		foreach( $this->items_ref as $result )
		{
			
#			$this->items[$cnt]->def = $result->def;
			
			if( $result->comment)
			{
				$this->items[$cnt]->trans = $this->items[$cnt]->mean;
				
			} else
			{
				
				$found = FALSE;
				foreach( $this->items_trans as $trans )
				{
					if( $trans->def == $result->def )
					{
						$found = TRUE;
						$result->trans = $trans->mean;
					}
				} // foreach
				
				if( ! $found )
				{
					$result->trans = '**MISSING**';
				}
			}
			$cnt++;
		} // foreach

	}//function

 /**
   * @param object transitem $item
   * @param string $path
   */
  function _getContents ( &$headsect, &$item, $path)
{
		$inhalt = file($path);
		$cnt = 0;
		$a_result = array();
		$isHeader = TRUE;

		foreach( $inhalt as $zeile )
		{
			$zeile = trim($zeile);
			
			$item[$cnt] = new transitem;
			if(   (strtolower(substr($zeile, 0, 6)) == 'define')
				&& (strtolower(substr($zeile, 0, 7)) != 'defined') )
			{ ### DEF FOUND
		#		echo '<div style="color:red"><pre>'.$zeile.'</pre></div>';
				$proc = trim(substr( $zeile, 6 ));//sub define
				$a_proc = explode( ',', $proc);
				$str_def = $a_proc[0];
				$str_mean = trim( substr( $proc, strlen($str_def) ) );
			//	$str_def = $this->_parse_def($str_def);
			$str_def = trim( substr( $str_def, 1) );###clean (
			$str_def = trim( substr( $str_def, 1, strlen( $str_def ) - 2 ) );###clean ' '

				if( trim( substr( $zeile, strlen($zeile)-1)) != ';')
				{ ### NOT end of line
					$str_mean = trim( substr( $str_mean, 2, strlen($str_mean)-3) );###clean ''
				}else
				{ ### end of line
		#			echo $str_mean.'<br />';
				$str_mean = trim( substr( $str_mean, 1) );###clean ,
	#			echo $str_mean.'<br />';
				$str_mean = trim(substr( $str_mean, 0, strlen($str_mean)-1));###clean ;
	#			echo $str_mean.'<br />';
				$str_mean = trim( substr( $str_mean, 1, strlen($str_mean)-2) );###clean ' )
	#			echo $str_mean.'<br />';
				$str_mean = trim(substr( $str_mean, 0, strlen($str_mean)-1));###clean '
	#			echo $str_mean.'<br />';
			##		$str_mean = $this->_parse_meaning($str_mean);
				} //if
			#	echo '<h3 style="color: blue"><pre>'.$str_def.'</pre></h3>' .'<h3 style="color: green"><pre>'.$str_mean.'</pre></h3>';
				$item[$cnt]->def = $str_def;
				$item[$cnt]->mean = (isset($item[$cnt]->mean)) ? $item[$cnt]->mean.$str_mean:$str_mean;
				$item[$cnt]->comment = FALSE;
				$isHeader = FALSE;
			$cnt++;
				
			} else if( substr($zeile,0,1) == '.')
			{ # first char is a dot... continuing last line
				$str_mean = substr( $str_mean, 2, strlen($str_mean)-5);###clean '');
				$this->items_ref[$cnt-1]->mean .= $str_mean;
				$cnt--;
			} else
			{ # nothing valid found... must be a comment
			#	echo '<pre>'.$zeile.'</pre>';
				if( $isHeader )
				{
					$headsect[] = $zeile;
				} else
				{
				$item[$cnt]->def = '';
				$item[$cnt]->mean = $zeile;
				$item[$cnt]->comment = TRUE;
			$cnt++;
				}
			} // if
		}//foreach
	return $item;
} // function
  

} //class

class transitem
{
	var $def		= '';
	var $mean	= '';
	var $trans	= '';
	var $comment	= FALSE;
}


?>